/**
 * 
 */
package com.cnc.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;



/**
 * 
 * 根据dataList显示多个checkbox
 * @author zzcclp
 *
 */
public class MultiCheckBoxesTag extends LtcmTag {

	private String name;
	private boolean disabled = false;
	private String selected;
	private String dataList;
	private String labelColumn;
	private String valueColumn;
	private Integer numPerLine = 5;

	private List dataMap = null;

	/**
	 * 
	 */
	public MultiCheckBoxesTag() {
		// TODO Auto-generated constructor stub
		super();
	}

	public String getDataList() {
		return dataList;
	}

	public void setDataList(String dataList) {
		this.dataList = dataList;
	}

	public List getDataMap() {
		return dataMap;
	}

	public void setDataMap(List dataMap) {
		this.dataMap = dataMap;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public String getLabelColumn() {
		return labelColumn;
	}

	public void setLabelColumn(String labelColumn) {
		this.labelColumn = labelColumn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getNumPerLine() {
		return numPerLine;
	}

	public void setNumPerLine(Integer numPerLine) {
		this.numPerLine = numPerLine;
	}

	public String getValueColumn() {
		return valueColumn;
	}

	public void setValueColumn(String valueColumn) {
		this.valueColumn = valueColumn;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public int doEndTag() throws JspException {

		try{
			HttpServletRequest request = (HttpServletRequest) pageContext
			.getRequest();
			if (null == dataList || dataList.equals("")){
				return super.doEndTag();
			}
			dataMap = (List)request.getAttribute(dataList);
			
			if (null == dataMap){
				return super.doEndTag();
			}
			
			StringBuffer sb = new StringBuffer("");
			int size = dataMap.size();

			for (int i = 0; i < size; i++) {
				Map record = (Map) dataMap.get(i);
				String optionValue = (String) record.get(valueColumn.trim());
				String labelValue = (String) record.get(labelColumn.trim());

				sb.append(" <input name=\"");
				sb.append(name);
				sb.append("\"");
				sb.append(" id=\"");
				sb.append(name);
				sb.append("\"");
				sb.append(" type=\"checkbox\"");
				sb.append(" value=\"");
				sb.append(optionValue);
				sb.append("\"");
				if (ischecked(optionValue)) {
					sb.append(" checked");
				}
				if (disabled) {
					sb.append(" disabled");
				}
				sb.append(" style=\"COLOR:#3aaedc;background-color:#e9f9ff;width:18px;\"");
				sb.append(" />");
				sb.append(labelValue);
				sb.append("&nbsp&nbsp");

				if ((i + 1) % numPerLine.intValue() == 0) {
					sb.append("<br>");
				}
			}
			pageContext.getOut().println(sb.toString());
			
			return super.doEndTag();
		} 
		catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e.toString());
		}

	}
	
	/**
	 * 在selected中查找optionValue值，如果存在则返回true，否则返回false
	 * @param optionValue
	 * @return
	 * @throws JspException
	 */
	private boolean ischecked(String optionValue) throws JspException {	
		String result = this.getSelectedValues();
		return result.indexOf(optionValue) != -1 ? true : false;
	}
	
	/*
	 * 根据页面传输过来的JSTL表达式，进行转换
	 */
	private String getSelectedValues() throws JspException {
		if ("".equals(selected)) {
			return new String();
		}
		String result = (String) TagUtil.evaluate("selected", this
				.getSelected(), String.class, this, pageContext);
		
		return result;
	}

}
